package com.interauto.Controller.runpage;

import com.interauto.bean.web.InterruntWeb;
import com.interauto.bean.web.RunCaseRuntWeb;
import com.interauto.bean.web.RunInterRuntWeb;
import com.interauto.service.kaklis.RunTaskProducer;
import com.interauto.service.web.RunTaskPageService;
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.safety.Whitelist;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * 运行结果相关哈
 */
@Controller
@RequestMapping(value = "/result")
public class TaskResultPage {
    @Autowired
    private RunTaskPageService runTaskPageService;
    @RequestMapping(value = "/caseresult",method = RequestMethod.GET)
    public ModelAndView caseresult(@RequestParam(value = "projectid") int projectid,
                                   @RequestParam(value = "page") int page){
        if(page<1){
            page=1;
        }
        List<RunCaseRuntWeb> runCaseRuntWebs=runTaskPageService.getRunCaseRuntWebs(projectid,page);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("caserusultpage");
        modelAndView.addObject("projectid",projectid);
        modelAndView.addObject("thispage",page);
        modelAndView.addObject("runCaseRuntWebs",runCaseRuntWebs);
        return modelAndView;
    }

    @RequestMapping(value = "/interresult",method = RequestMethod.GET)
    public ModelAndView interresult(@RequestParam(value = "projectid") int projectid,
                                    @RequestParam(value = "caserunid") int caserunid){
        List<RunInterRuntWeb> runInterRuntWebs=runTaskPageService.getRunInterRuntWebs(caserunid);
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("interresultpage");
        modelAndView.addObject("projectid",projectid);
        modelAndView.addObject("runinterruntwebs",runInterRuntWebs);
        return modelAndView;
    }

    @RequestMapping(value = "/interresultinfo",method = RequestMethod.GET)
    public ModelAndView interresultinfo(@RequestParam(value = "projectid") int projectid,
                                        @RequestParam(value = "resultid") int resultid){
        InterruntWeb interruntWeb= runTaskPageService.getInterruntWeb(resultid);
        if(interruntWeb !=null){
            String resinfo=interruntWeb.getResinfo();
            Whitelist whitelist = Whitelist.none();
            if(StringUtils.isNoneEmpty(interruntWeb.getMessage())){ //处理返回的内容可能有xss问题
                interruntWeb.setMessage(Jsoup.clean(interruntWeb.getMessage(),whitelist));
            }
            if(StringUtils.isNoneEmpty(resinfo)){  //处理返回的内容可能有xss问题
                interruntWeb.setResinfo(Jsoup.clean(resinfo,whitelist));
            }
        }
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.addObject("projectid",projectid);
        modelAndView.addObject("interruntWeb",interruntWeb);
        modelAndView.setViewName("interresultinfopage");
        return modelAndView;
    }
}
